/*
 * @(#)CacheResultAcceptor.java $version 2010. 8. 11
 *
 * Copyright 2007-2010 NHN Corp. All rights Reserved. 
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.nhncorp.usf.core.service.supplement;

import com.nhncorp.usf.core.cache.CacheKey;
import com.nhncorp.usf.core.cache.CacheManager;
import com.nhncorp.usf.core.cache.CacheStatusException;
import com.nhncorp.usf.core.cache.ResultCacheKey;
import com.nhncorp.usf.core.config.runtime.Method;
import com.nhncorp.usf.core.service.ExecutionException;
import com.nhncorp.usf.core.service.MethodReturn;
import com.nhncorp.usf.core.service.ServiceExecutionContext;

/**
 * @author Web Platform Development Team.
 */
public class CacheResultAcceptor implements PostSupplement {

    /**
     * @param context ServiceExecutionContext
     * @return boolean
     * @throws ExecutionException the ExecutionException
     * @see com.nhncorp.usf.core.service.supplement.PostSupplement#afterExecute(com.nhncorp.usf.core.service.ServiceExecutionContext)
     */
    public boolean afterExecute(ServiceExecutionContext context) throws ExecutionException {
        Method method = context.getMethod();
        Object result = context.getAttribute(method.getReturnId());

        if (context.getReturnType() != MethodReturn.ReturnType.failure && context.isEnableAddCache()) {
            try {
                CacheKey key = new ResultCacheKey(context.getBo().getId(), context.getMethod().getName(),
                                                  context.getMethodParameter());
                CacheManager.getInstance().addCache(key, result);
            } catch (CacheStatusException e) {
                throw new ExecutionException(e);
            }
        }

        return true;
    }
}
